package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import com.yc.shopping.dao.GoodDao;
import com.yc.shopping.dao.GoodTypeDao;
import com.yc.shopping.util.ShoppingUtil;
import org.eclipse.swt.widgets.Label;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;

public class Dialog_AddGoods extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_gname;
	private Text text_gcode;
	private Text text_unit;
	private Text text_weight;
	private Text text_gdate;
	private Text text_price;
	private Text text_saleprice;
	private Text text_gmark;
	private Combo combo;
	private String fileName;//文件上传的图片名称
	
	GoodTypeDao typeDao = new GoodTypeDao();
	GoodDao dao = new GoodDao();

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Dialog_AddGoods(Shell parent, int style) {
		super(parent, style);
		setText("添加商品信息");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(597, 782);
		shell.setText(getText());
		//对话框居中
		ShoppingUtil.centerWindows(shell);
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(36, 102, 90, 24);
		label.setText("商品类型:");
		
		//下拉列表作为全局变量
		combo = new Combo(shell, SWT.NONE);
		combo.setItems(new String[] {"aa", "bb", "cc", "asdasd"});
		combo.setBounds(132, 99, 137, 32);
		combo.select(0);//默认显示下拉列表中第几个值
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setBounds(296, 102, 90, 24);
		label_1.setText("商品名称:");
		
		text_gname = new Text(shell, SWT.BORDER);
		text_gname.setBounds(392, 102, 147, 30);
		
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setBounds(36, 183, 90, 24);
		label_2.setText("条形码:");
		
		text_gcode = new Text(shell, SWT.BORDER);
		text_gcode.setBounds(130, 177, 137, 30);
		
		Label label_3 = new Label(shell, SWT.NONE);
		label_3.setBounds(296, 183, 90, 24);
		label_3.setText("商品单位:");
		
		text_unit = new Text(shell, SWT.BORDER);
		text_unit.setBounds(397, 180, 152, 30);
		
		Label label_4 = new Label(shell, SWT.NONE);
		label_4.setBounds(36, 266, 90, 24);
		label_4.setText("商品净重:");
		
		text_weight = new Text(shell, SWT.BORDER);
		text_weight.setBounds(132, 260, 137, 30);
		
		Label label_5 = new Label(shell, SWT.NONE);
		label_5.setBounds(296, 266, 90, 24);
		label_5.setText("保 质 期：");
		
		text_gdate = new Text(shell, SWT.BORDER);
		text_gdate.setBounds(392, 260, 147, 30);
		
		Label label_6 = new Label(shell, SWT.NONE);
		label_6.setBounds(36, 344, 90, 24);
		label_6.setText("进货价:");
		
		text_price = new Text(shell, SWT.BORDER);
		text_price.setBounds(132, 344, 137, 30);
		
		Label label_7 = new Label(shell, SWT.NONE);
		label_7.setBounds(296, 344, 90, 24);
		label_7.setText("销售价:");
		
		text_saleprice = new Text(shell, SWT.BORDER);
		text_saleprice.setBounds(392, 344, 147, 24);
		
		Label label_8 = new Label(shell, SWT.NONE);
		label_8.setBounds(36, 459, 90, 24);
		label_8.setText("商品描述:");
		
		text_gmark = new Text(shell, SWT.BORDER | SWT.MULTI);
		text_gmark.setBounds(132, 411, 130, 218);
		
		Label label_9 = new Label(shell, SWT.NONE);
		label_9.setBounds(283, 459, 90, 24);
		label_9.setText("商品图片:");
		
		Label label_image = new Label(shell, SWT.CENTER);
		label_image.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				FileDialog fd = new FileDialog(shell);
				fd.setFilterExtensions(new String[] {"*.jpg","*.png","*.jpeg","*.*"} );//后缀名设置
				String select = fd.open(); //返回图片的绝对路径
				System.out.println(select);
				if(select == null || "".equals(select)) {
					ShoppingUtil.showMessage(shell, "消息提示", "未选中图片");
				}
				
				
				try {
					File file = new File(select);
					fileName = file.getName();
					InputStream in = new FileInputStream(file);
					Image image = new Image(Display.getDefault(),in);
					label_image.setImage(image);
					
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}
		});
		label_image.setImage(SWTResourceManager.getImage(Dialog_AddGoods.class, "/images/zanwu.jpg"));
		label_image.setBounds(392, 411, 189, 218);
		
		Label label_upload = new Label(shell, SWT.NONE);
		label_upload.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		label_upload.setBounds(420, 653, 147, 24);
		label_upload.setText("双击上传图片");
		
		Button button_add = new Button(shell, SWT.NONE);
		//添加
		button_add.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//从界面上获取数据
				String type = combo.getText().trim();//1-鱼类 只需要编号
				String tid = type.split("-")[0];
				if(fileName == null || fileName.equals("")) {
					ShoppingUtil.showMessage(shell, "消息提示", "未上传图片");
					return;
				}
				
				String gname = text_gname.getText().trim();
				String gcode = text_gcode.getText().trim();
				String gdate = text_gdate.getText().trim();
				String price = text_price.getText().trim();
				String saleprice = text_saleprice.getText().trim();
				String gmark = text_gmark.getText().trim();
				String unit = text_unit.getText().trim();
				String weight = text_weight.getText().trim();
				String gimage = "images/" + fileName;
				
				try {
					int result = dao.add(gname, gcode, Integer.parseInt(tid), gmark, gimage, unit,
							weight, 0, gdate, Float.parseFloat(price), Float.parseFloat(saleprice));
					if( result <= 0) {
						ShoppingUtil.showMessage(shell, "错误提示", "商品录入失败");
						return;
					}
					//1.清空所有的文本框图片
					text_gcode.setText("");
					text_gdate.setText("");
					text_gmark.setText("");
					text_gname.setText("");
					text_price.setText("");
					text_saleprice.setText("");
					text_unit.setText("");
					text_weight.setText("");
					//图片清空
					label_image.setImage(SWTResourceManager.getImage(Dialog_AddGoods.class, "/images/zanwu.jpg"));
					label_image.setBounds(392, 411, 189, 218);
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		button_add.setBounds(257, 682, 114, 34);
		button_add.setText("添加");
		//所有组件加载完成后，再进行数据加载
		showtypes();

	}
	
	/*
	 * 将类型显示在下拉列表
	 */
	
	private void showtypes() {
		try {
			List<Map<String,Object>> list = typeDao.findAll();
		
			String[] infos = new String[list.size()];
			for(int i = 0;i<list.size();i++) {
				Map<String,Object> m =list.get(i);
				//下拉列表 1-肉类
				infos[i] = ShoppingUtil.objToString(m.get("TID") + "-" + ShoppingUtil.objToString(m.get("TNAME")));
				
			}
			//设置到下拉列表中
			combo.removeAll();//清除下拉列表数据
			combo.setItems(infos);	
			combo.select(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}
}
